<script setup lang="ts">
import { getHomeBanner, getHomeCategoryApi, getHomeHot } from '@/services/home'
import CustomNavbar from './components/CustomNavbar.vue'
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
import type { HomeBannerResult, HomeCategoryItem, HomeHotItem } from '@/types/home'
import CategoryPanel from './components/CategoryPanel.vue'
import HotPannel from './components/HotPannel.vue'
import pageSkeleton from './components/pageSkeleton.vue'
import { useGuessList } from '@/composables'
// 轮播图数组
const bannerList = ref<HomeBannerResult[]>([])

// 前台分类数组
const categoryList = ref<HomeCategoryItem[]>([])

// 热门数组
const hotList = ref<HomeHotItem[]>([])

// 下拉触发按钮
const isTriggered = ref(false)

// 刷新列表猜你喜欢
const refresh = ref(true)

// 加载中骨架屏
const loading = ref(false)

// 获取轮播图数据
const getHomeBannerData = async () => {
  const res = await getHomeBanner()
  bannerList.value = res.result
}

// 获取前台数据接口
const getHomeCategoryData = async () => {
  const res = await getHomeCategoryApi()
  categoryList.value = res.result
}

// 获取热门数据
const getHomeHotData = async () => {
  const res = await getHomeHot()
  hotList.value = res.result
}

const { guessRef, onScrollToLower } = useGuessList()

// 下拉刷新
const onRefresherRefresh = async () => {
  isTriggered.value = true
  refresh.value = false
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), getHomeHotData()])
  isTriggered.value = false
  refresh.value = true
}

onLoad(async () => {
  loading.value = true
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), getHomeHotData()])
  loading.value = false
})
</script>

<template>
  <CustomNavbar />
  <scroll-view
    @refresherrefresh="onRefresherRefresh"
    refresher-enabled
    :refresher-triggered="isTriggered"
    @scrolltolower="onScrollToLower"
    class="scroll-view"
    scroll-y
  >
    <pageSkeleton v-if="loading" />
    <template v-else>
      <XtxSwiper :list="bannerList" />
      <CategoryPanel :list="categoryList" />
      <HotPannel :list="hotList" />
      <XtxGuess ref="guessRef" v-if="refresh" />
    </template>
  </scroll-view>
</template>

<style lang="scss">
page {
  background-color: #f7f7f7;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.scroll-view {
  flex: 1;
}
</style>
